﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Catalog;

public partial class sysadmin_CourseManager_EditCatalog :PowerCreator.LMS.Web.Base.BasePage
{
    Catalog opCatalog = new Catalog();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            //BinderDDL();

            string catalogid = Request.QueryString["CatalogID"].ToString();
            ViewState["CatalogID"] = catalogid;

            VOTCatalog vo = opCatalog.Load(int.Parse(catalogid));
            txt_CatalogName.Text = vo.CatalogName;
            txt_CatalogDescription.Text = vo.CatalogContent;
            //ddl_Catalog.SelectedValue = vo.CatalogFatherId.ToString();
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_EditCatalog_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Dep_Name.Text = GetLanageValueByKey("lbl_EditCatalog_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_parentcatalog.Text = GetLanageValueByKey("lbl_EditCatalog_parentcatalog", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_catalog_description.Text = GetLanageValueByKey("lbl_EditCatalog_description", PowerCreator.LMS.Language.LanagetItemType.Page);

        rfv_CatalogName.ErrorMessage = GetLanageValueByKey("AddCatalog_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Save.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private bool CheckPageData()
    {
        string search = string.Format("CatalogName='{0}' and CatalogID <> {1}", txt_CatalogName.Text, ViewState["CatalogID"]);

        if (opCatalog.QueryCatalogCount(search) != 0)
        {
            //lbl_addcatalog_msg_catalogname.Text = "己经存在该专业信息，请重新更改专业名称!";
            lbl_msg_description.Text = GetLanageValueByKey("AddCatalog_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
        return true;
    }

    //private void BinderDDL()
    //{
    //    string search = "";
    //    DataTable dt = opCatalog.QueryCatalog(search);
    //    ArrayList list = opCatalog.GetCatalogTree(dt);
    //    ddl_Catalog.DataSource = list;
    //    ddl_Catalog.DataTextField = "Text";
    //    ddl_Catalog.DataValueField = "Value";
    //    ddl_Catalog.DataBind();

    //    ListItem li = new ListItem();
    //    li.Text = GetLanageValueByKey("AddCatalog_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    li.Value = "0";
    //    ddl_Catalog.Items.Insert(0, li);
    //}

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTCatalog vo = opCatalog.Load(int.Parse(ViewState["CatalogID"].ToString()));
        vo.CatalogContent = txt_CatalogDescription.Text;
        //if (int.Parse(ddl_Catalog.SelectedValue.ToString()) != 0)
        //{
        //    string sonLayer = vo.CatalogLayer;
        //    VOTCatalog fatherVO = new VOTCatalog();
        //    fatherVO = opCatalog.Load(int.Parse(ddl_Catalog.SelectedValue.ToString()));
        //    string fatherLayer = fatherVO.CatalogLayer;
        //    if (compareLayer(sonLayer, fatherLayer))
        //    {
        //        //lbl_err_parentcatalog.Text = "不能选择本专业的子级专业作为父专业，如却有需要，请先处理下边的子专业！";
        //        lbl_msg_description.Text = GetLanageValueByKey("EditorCatalog_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return;
        //    }
        //}
        //if (vo.CatalogFatherId != int.Parse(ddl_Catalog.SelectedValue.ToString()))
        //{
        //    vo.CatalogFatherId = int.Parse(ddl_Catalog.SelectedValue.ToString());
        //    vo.CatalogFatherId = int.Parse(ddl_Catalog.SelectedValue.ToString());
        //    vo.CatalogLayer = opCatalog.QueryDepartmentLayer(vo.CatalogFatherId);
        //}
        
        vo.CatalogName = txt_CatalogName.Text;
        if (opCatalog.UpdateCatalog(vo))
        {
            string url = "Catalog.aspx";
            Script.RedirectURL(url);
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Catalog.aspx";
        Script.RedirectURL(url);
    }

    /// <summary>
    /// 对比当前专业的layer与新的父级节点layer
    /// </summary>
    /// <param name="sonlayer">当前专业的layer</param>
    /// <param name="fatherlayer">新的父级节点layer</param>
    /// <returns></returns>
    private bool compareLayer(string sonlayer,string fatherlayer)
    {
        int index = sonlayer.IndexOf("00");
        string sonlayer_new = sonlayer.Remove(index);
        string fatherlayer_new = fatherlayer.Remove(index);
        if (sonlayer_new == fatherlayer_new)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
